<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Natural keys &mdash; django-serializable v0.1 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="top" title="django-serializable v0.1 documentation" href="../index.html" />
    <link rel="next" title="Writing serializable models" href="models.html" />
    <link rel="prev" title="Serialization support for Django models" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="models.html" title="Writing serializable models"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../index.html" title="Serialization support for Django models"
             accesskey="P">previous</a> |</li>
        <li><a href="../index.html">django-serializable v0.1 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="natural-keys">
<h1>Natural keys<a class="headerlink" href="#natural-keys" title="Permalink to this headline">¶</a></h1>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">For more information on the <em>natural keys</em>, see <a class="reference external" href="http://docs.djangoproject.com/en/dev/topics/serialization/#natural-keys">django doc</a></p>
</div>
<p>The concept of <em>natural key</em> and its interest for serialization is very simple... here is an example.</p>
<p>Consider the two models</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Book</span> <span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">)</span> <span class="p">:</span>
    <span class="n">author</span> <span class="o">=</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="n">Author</span><span class="p">)</span>
    <span class="n">title</span> <span class="o">=</span> <span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Author</span> <span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">)</span> <span class="p">:</span>
    <span class="n">firstname</span> <span class="o">=</span> <span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">lastname</span> <span class="o">=</span> <span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
</pre></div>
</div>
<p>and you don&#8217;t want a serialized book to look like :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">{</span><span class="n">author</span><span class="p">:</span> <span class="mi">56</span><span class="p">,</span> <span class="n">title</span><span class="p">:</span> <span class="s">&#39;East of Eden&#39;</span><span class="p">}</span>
</pre></div>
</div>
<p>with an author that is only a number in your database (i.e a foreign key), but rather :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">{</span><span class="n">author</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;John&#39;</span><span class="p">,</span> <span class="s">&#39;Steinbeck&#39;</span><span class="p">),</span> <span class="n">title</span><span class="p">:</span> <span class="s">&#39;East of Eden&#39;</span><span class="p">}</span>
</pre></div>
</div>
<p>Then, you should tell Django to serialize foreign keys to a so-called <em>natural key</em> (by default, it will use the foreign object&#8217;s <em>primary key</em>).
Of course, Django needs to know how to map a <em>primary key</em> to a <em>natural key</em>, because the primary key of the foreign object is all Django has.
This is done by providing a method <tt class="xref docutils literal"><span class="pre">natural_key</span> </tt> to the &#8220;foreign class&#8221;.</p>
<p>In our case, the <em>natural key</em> we want is <em>(firstname, lastname)</em>, therefore <tt class="xref docutils literal"><span class="pre">Author</span></tt> must provide a method <tt class="xref docutils literal"><span class="pre">natural_key</span> </tt> that simply works like this :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">an_author</span><span class="o">.</span><span class="n">natural_key</span><span class="p">()</span>
<span class="go">(&#39;John&#39;, &#39;Steinbeck&#39;)</span>
</pre></div>
</div>
<p>Same goes, if you want to deserialize the dictionnary :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">{</span><span class="n">author</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;John&#39;</span><span class="p">,</span> <span class="s">&#39;Steinbeck&#39;</span><span class="p">),</span> <span class="n">title</span><span class="p">:</span> <span class="s">&#39;East of Eden&#39;</span><span class="p">}</span>
</pre></div>
</div>
<p>to a <tt class="xref docutils literal"><span class="pre">Book</span></tt>, you should also tell Django to use the <em>natural key</em> for the foreign keys of <tt class="xref docutils literal"><span class="pre">Book</span></tt>.</p>
<p>Of course Django needs to map this <em>natural key</em> to a <em>primary key</em>, so it can properly deserialize a <em>natural key</em> to an actual foreign key.
This is done by providing the default manager of the &#8220;foreign class&#8221; with a method <tt class="xref docutils literal"><span class="pre">get_by_natural_key</span></tt>.</p>
<p>In our case, this method should work like that :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">an_author</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get_by_natural_key</span><span class="p">(</span><span class="s">&#39;John&#39;</span><span class="p">,</span> <span class="s">&#39;Steinbeck&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">an_author</span><span class="o">.</span><span class="n">id</span>
<span class="go">56</span>
</pre></div>
</div>
<p>And it should return the author with <em>primary key</em> &#8216;56&#8217; (assuming that this key corresponds with &#8216;Steinbeck&#8217; in our database).</p>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h4>Previous topic</h4>
            <p class="topless"><a href="../index.html"
                                  title="previous chapter">Serialization support for Django models</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="models.html"
                                  title="next chapter">Writing serializable models</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/doc_pages/natural_keys.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="models.html" title="Writing serializable models"
             >next</a> |</li>
        <li class="right" >
          <a href="../index.html" title="Serialization support for Django models"
             >previous</a> |</li>
        <li><a href="../index.html">django-serializable v0.1 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2010, Sebastien Piquemal.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.4.
    </div>
  </body>
</html>